package cn.tedu.tmall.front.mall.dao.search.impl;

import cn.tedu.tmall.common.pojo.po.GoodsSearchPO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodsElasticsearchRepository
        extends ElasticsearchRepository<GoodsSearchPO, Long> {

    // 【实际应用】【高亮】【自定义搜索】使用同一个关键词参数，根据title或brief或keywords字段搜索，且结果中的title属性需要高亮
    @Query("{\n" +
            "    \"bool\": {\n" +
            "      \"should\": [\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"title\": \"?0\"\n" +
            "          }\n" +
            "        },\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"brief\": \"?0\"\n" +
            "          }\n" +
            "        },\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"keywords\": \"?0\"\n" +
            "          }\n" +
            "        }\n" +
            "      ]\n" +
            "    }\n" +
            "  }")
    @Highlight(fields = {@HighlightField(name = "title")},
            parameters = @HighlightParameters(preTags = "<font style='color: red;'>", postTags = "</font>"))
    SearchPage<GoodsSearchPO> search(String keyword, Pageable pageable);

    // 【基于JPA风格的方法名的搜索】根据title搜索
    List<GoodsSearchPO> queryByTitle(String keyword);

    // 【基于JPA风格的方法名的搜索】根据title或brief搜索
    List<GoodsSearchPO> queryByTitleOrBrief(String title, String brief);

    // 【高亮】【基于JPA风格的方法名的搜索】根据title搜索，并分页
    @Highlight(fields = {@HighlightField(name = "title")},
        parameters = @HighlightParameters(preTags = "<font style='color: red;'>", postTags = "</font>"))
    SearchPage<GoodsSearchPO> queryByTitleOrBrief(String keyword, String brief,Pageable pageable);

    // 【自定义搜索】根据title搜索
    @Query("{\n" +
            "    \"match\": {\n" +
            "      \"title\": \"?0\"\n" +
            "    }\n" +
            "  }")
    List<GoodsSearchPO> customQueryByTitle(String keyword);

    // 【自定义搜索】根据title或brief搜索
    @Query("{\n" +
            "    \"bool\": {\n" +
            "      \"should\": [\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"title\": \"?0\"\n" +
            "          }\n" +
            "        },\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"brief\": \"?1\"\n" +
            "          }\n" +
            "        }\n" +
            "      ]\n" +
            "    }\n" +
            "  }")
    List<GoodsSearchPO> customQueryByTitleOrBrief(String title, String brief);

}
